package com.longdaji.decoration.ui.goodslist.select;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseFragment;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.GoodsBean;
import com.longdaji.decoration.ui.detail.GoodsDetailActivity;
import com.longdaji.decoration.utils.LinearManagerDivider;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by longtao on 2018/1/14.
 */

public class GoodsSelectFragment extends BaseFragment implements GoodsSelectContract.View, SwipeRefreshLayout.OnRefreshListener {
    private static final String TAG = GoodsSelectFragment.class.getSimpleName();
    @BindView(R.id.recycler_view_goods_select)
    RecyclerView mRecyclerViewGoodsSelect;
    @BindView(R.id.tv_no_data)
    TextView mTvNoData;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    @Inject
    GoodsSelectPresenter mPresenter;
    private String mCateId;
    private GoodsSelectAdapter mAdapter;
    private String mOrderBy;

    @Inject
    public GoodsSelectFragment() {
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_goods_list_select;
    }

    @Override
    protected void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mCateId = bundle.getString("cate_id");
            mOrderBy = bundle.getString("order_by");
        }
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        mSwipeRefresh.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        mSwipeRefresh.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        mSwipeRefresh.setOnRefreshListener(this);

        mPresenter.setView(this);
        Log.d(TAG, "GoodsSelectFragment mCateId=" + mCateId);
        initReclerView();
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            initData();
        }
    }

    @Override
    public void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    public static GoodsSelectFragment newInstance(Bundle bundle) {
        GoodsSelectFragment contentFragment = new GoodsSelectFragment();
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    private void initReclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerViewGoodsSelect.setLayoutManager(layoutManager);
        mAdapter = new GoodsSelectAdapter(getActivity(), new ArrayList<GoodsBean.Goods>(), new GoodsSelectAdapter.GoodsSelectItemListener() {
            @Override
            public void onGoodsItemClick(GoodsBean.Goods goodItem) {
                Intent intent = GoodsDetailActivity.getStartIntent(getActivity());
                intent.putExtra("goods_id", goodItem.getGoodsId());
                getActivity().startActivity(intent);
            }

            @Override
            public void onCollectClick(int position, String goodId) {
                if (TextUtils.isEmpty(Account.getInstance().getUserid())) {
                    toast("请先登录账户！");
                    return;
                }
                mPresenter.collectCurGoods(Account.getInstance().getUserid(), goodId, position);
            }

            @Override
            public void onDisCollectClick(int position, String goodId) {
                if (TextUtils.isEmpty(Account.getInstance().getUserid())) {
                    toast("请先登录账户！");
                    return;
                }
                mPresenter.disCollectCurGoods(Account.getInstance().getUserid(), goodId, position);
            }
        });
        mRecyclerViewGoodsSelect.setAdapter(mAdapter);
        mRecyclerViewGoodsSelect.addItemDecoration(new LinearManagerDivider(getActivity(), LinearLayoutManager.VERTICAL, 40, ContextCompat.getColor(getActivity(), R.color.white)));
        //initData();
    }

    private void initData() {
        mSwipeRefresh.setRefreshing(true);
        mPresenter.getGoodsList(mCateId, mOrderBy, Account.getInstance().getUserid());
    }

    @Override
    public void showGoodsList(List<GoodsBean.Goods> goodsList) {
        mSwipeRefresh.setRefreshing(false);
        mTvNoData.setVisibility(View.GONE);
        mRecyclerViewGoodsSelect.setVisibility(View.VISIBLE);
        mAdapter.setData(goodsList);
    }

    @Override
    public void showGoodsListNone() {
        mSwipeRefresh.setRefreshing(false);
        mTvNoData.setVisibility(View.VISIBLE);
        mRecyclerViewGoodsSelect.setVisibility(View.GONE);
    }

    @Override
    public void onRefresh() {
        mSwipeRefresh.setRefreshing(true);
        mPresenter.getGoodsList(mCateId, mOrderBy, Account.getInstance().getUserid());
    }

    @Override
    public void showCollectSuccess(int position) {
        toast("收藏成功");
        if (mAdapter.getItemByPosition(position) != null) {
            mAdapter.getItemByPosition(position).setIsCollection("1");
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showCollectFail() {
        toast("收藏失败");
    }

    @Override
    public void showDisCollectSuccess(int position) {
        if (mAdapter.getItemByPosition(position) != null) {
            mAdapter.getItemByPosition(position).setIsCollection("0");
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDisCollectFail() {
        toast("取消藏失败");
    }

}
